var dataUrl = null;
$(function () {
    init();

    $("form").validate({
        submitHandler: function (form) {// 必须写在验证前面，否则无法ajax提交
            if (selectNotCheckCountNum()) {
                layer.alert('存在未评分的检查项！', 3);
                return false;
            }
            //jquery获取复选框值
            $(form).ajaxSubmit({
                dataType: "text",
                success: function (data) {
                    if (data == "success") {
                        parent.layer.msg('开具成功！');
                        parent.location.href = '/taskManage/inspectTask';
                    } else {
                        layer.alert('开具失败！', 3);
                    }
                },
                error: function (data) {
                    layer.alert('开具失败！', 3);
                }
            });
        }
        ,
        rules: {
            "taskCorpId":
                {
                    required: true,
                }
            ,
            "taskPersonId":
                {
                    required: true,
                }
            ,
        }
        ,
        messages: {
            "taskCorpId":
                {
                    required: "请选择整改单位",
                }
            ,
            "taskPersonId":
                {
                    required: "请选择整改人",
                }
            ,
        }
        ,
        errorPlacement: function (error, element) {// 自定义提示错误位置
            $(".l_err").css('display', 'block');
            // element.css('border','3px solid #FFCCCC');
            $(".l_err").html(error.html());
        }
        ,
        success: function (label) {// 验证通过后
            $(".l_err").css('display', 'none');
        }
    })
    ;
});

function init() {
    // dataUrl = $("#dataUrl").val();
    dataUrl = "/modules";
    getResponsibilityCropList();

}

/**
 * 责任单位下拉列表
 */
function getResponsibilityCropList() {
    var url = dataUrl + "/corp/all";
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].caption) + "</option>";
                }
                $("#responsibilityCrop").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}

/**
 * 责任人下拉列表
 * @param cropId 单位id
 */
function getResponsibilityPersonList(corpId) {
    debugger
    var url = dataUrl + "/corp/persons?corpId=" + corpId;
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].name) + "</option>";
                }
                $("#responsibilityPerson").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}

function selectNotCheckCountNum() {
    var check=true;
    var url = dataUrl + "/taskManage/selectNotCheckCountNum?taskId=" + $("#taskId").val();
    $.ajax({
        url: url,
        method: "get",
        dataType: "text",
        cache:false,
        async:false,
        success: function (result) {
            if (result == 0) {
                check= false;
            } else {
                check= true;
            }
        }
    });
    return check;
}